<?php

$root = '../../..';
include $root.'/apps/common/lib/class.cache.php';
// SPLIT REQUEST
list($browser, $theme, $app, $widgets, $version, $debug) = explode('/', $_SERVER['QUERY_STRING'], 6);
header('content-type: application/javascript');
if (($js = cache::get($browser . '_' . $theme . '_' . $app . '_' . $widgets . '_' . $version . '.js')) && !$debug) {
    die($js);
} else {
    $info = <<<COMMENTS
/*
BROWSER : $browser
THEME   : $theme
APP     : $app
WIDGETS : $widgets
VERSION : $version
DEBUG   : $debug
*/\n
COMMENTS;
// JS LANGUAGE
    $js .= 'var _lang = {"close":"' . dgettext('common', 'Close')
            . '", "ok":"' . dgettext('common', 'Ok')
            . '", "cancel":"' . dgettext('common', 'Cancel') . '"};';
// THEME JS FILES
    foreach (@glob($root . '/themes/' . $theme . '/js/*.js') as $i)
        $js .= file_get_contents($i);
// APPLICATION TEMPLATE JS
    $js .= @ file_get_contents($root . '/apps/' . $app . '/template/js/' . $app . '.js');
// PAGE ITEMS JS
    foreach (explode(',', $widgets, 20) as $app)
        foreach (@glob($root . '/apps/' . $app . '/template/js/_item.js') as $i)
            $js .= file_get_contents($i);
// BROWSER HACK
    $js .= @ file_get_contents($root . '/themes/' . $theme . '/browsers/' . $browser . '.js');

    if ($debug)
        die($info . $js);
    else
        $js = preg_replace(array(
                    '!//[^\n\r]+!',
                    //'/[\r\n\t\s]+/s',
                    '/^[\r\n\t\s]+/m',
                    '#/\*.*?\*/#',
                    '/[\s]*([\{\},;:\=\+\-\?\|\&])[\s]*/',
                    '/^\s+/'
                        //'/\);/'
                        ),
                        array(
                            '',
                            //' ',
                            '',
                            '',
                            '\1',
                            ''
                        //");\n"
                        ),
                        $js);
    cache::set($browser . '_' . $theme . '_' . $app . '_' . $widgets . '_' . $debug . '.js', $js);
    die($js);
}
?>